package guoanmaker.operator.business.service;

import guoanmaker.operator.business.model.OperatorRoundPhoto;
import guoanmaker.operator.business.repository.OperatorRoundPhotoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.transaction.Transactional;

/**
 * @author huZhaoyu 
 * @ClassName: OperatorRoundPhotoService 
 * @Description: OperatorRoundPhotoService
 * @date 2017/6/28 上午9:41 
 */
@Service
public class OperatorRoundPhotoService implements OperatorRoundPhotoServiceInterface {
    private final OperatorRoundPhotoRepository operatorRoundPhotoRepository;
    @PersistenceContext
	private EntityManager manager;
    @Autowired
    public OperatorRoundPhotoService(OperatorRoundPhotoRepository operatorRoundPhotoRepository) {
        this.operatorRoundPhotoRepository = operatorRoundPhotoRepository;
    }

    @Override
    public OperatorRoundPhoto saveRoundPhoto(OperatorRoundPhoto operatorRoundPhoto) {
        return operatorRoundPhotoRepository.save(operatorRoundPhoto);
    }

    @Override
    public List<OperatorRoundPhoto> getAllRoundphoto() {
        return operatorRoundPhotoRepository.findAll();
    }

    @Override
    public List<OperatorRoundPhoto> getAllRoundPhotoByPage(String cityCode) {
       
        return operatorRoundPhotoRepository.findBycityCode(cityCode);
    }

    @Override
    public void deleteRoundPhotoById(String id) {
        operatorRoundPhotoRepository.delete(id);
    }

    @Override
    public OperatorRoundPhoto getRoundPhotoById(String id) {
        return operatorRoundPhotoRepository.findOne(id);
    }

    @Override
    public Page<OperatorRoundPhoto> getAllRoundPhotoByWeightAndPage(Integer page, Integer size) {
        Sort sort = new Sort(Sort.Direction.DESC, "weight");
        Pageable pageable = new PageRequest(page, size, sort);
        return operatorRoundPhotoRepository.findAll(pageable);
    }
    
    @Transactional
	  public void delectAllBycitycode(String cityCode){
		  String jpql="delete  from OperatorRoundPhoto  u where u.cityCode=:cityCode";
		  Query query2=manager.createQuery(jpql);//查询商品提供的服务区域
			query2.setParameter("cityCode", cityCode);
			query2.executeUpdate();
		  
	  }
}
